package com.ant.myagile.backlog

import org.springframework.dao.DataIntegrityViolationException

import com.ant.myagile.Project
import com.ant.myagile.UserStoryService

class UserStoryController {

	static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
	UserStoryService userStoryService = new UserStoryService();

	def index() {
		//        redirect(action: "list", params: params)
		[userStoryList : UserStory.list()]
		redirect(action:"listUserStory", controller: "userStory")
	}

	def createUserStory(){
		def projects = Project.list();
		[projects: projects]
	}

	def addUserStory(){
		if (params.value?.isNumber()){
			params.project = Project.get(params.project)
			def userStory =  new UserStory(params)
			userStory.save(failOnError:true)
			[userStory : userStory]
			flash.message = "User Story ${userStory.id} created"
		}
		else{
			flash.message = "Value must be number"
		}
		redirect(action:"createUserStory", controller: "userStory")
	}

	def listUserStory(){
		[userStoryList : UserStory.list()]
	}

	def deleteUserStory(){
		def us = UserStory.findById(params.id?.toInteger())
		us.delete()
		redirect(action:"listUserStory", controller: "userStory")
	}

	def editUserStory(){
		def us = UserStory.findById(params.id?.toInteger())
		[userStory : us]
	}

	def updateUserStory(){
		def us = UserStory.get(params.id)
		us.name = params.name
		us.description = params.description
		us.value = params.value?.toInteger()
		us.status = params.status
		us.save(flush: true)
		redirect(action:"listUserStory", controller: "userStory")
	}


	def list(Integer max) {
		params.max = Math.min(max ?: 10, 100)
		[userStoryInstanceList: UserStory.list(params), userStoryInstanceTotal: UserStory.count()]
	}

	def create() {
		[userStoryInstance: new UserStory(params)]
	}

	def save() {
		def userStoryInstance = new UserStory(params)
		
		if (!userStoryInstance.save(flush: true)) {
			render(view: "create", model: [userStoryInstance: userStoryInstance])
			return
		}

		flash.message = message(code: 'default.created.message', args: [
			message(code: 'userStory.label', default: 'UserStory'),
			userStoryInstance.id
		])
		redirect(action: "show", id: userStoryInstance.id)
	}

	def show(Long id) {
		def userStoryInstance = UserStory.get(id)
		if (!userStoryInstance) {
			flash.message = message(code: 'default.not.found.message', args: [
				message(code: 'userStory.label', default: 'UserStory'),
				id
			])
			redirect(action: "list")
			return
		}

		[userStoryInstance: userStoryInstance]
	}

	def edit(Long id) {
		def userStoryInstance = UserStory.get(id)
		if (!userStoryInstance) {
			flash.message = message(code: 'default.not.found.message', args: [
				message(code: 'userStory.label', default: 'UserStory'),
				id
			])
			redirect(action: "list")
			return
		}

		[userStoryInstance: userStoryInstance]
	}

	def update(Long id, Long version) {
		def userStoryInstance = UserStory.get(id)
		if (!userStoryInstance) {
			flash.message = message(code: 'default.not.found.message', args: [
				message(code: 'userStory.label', default: 'UserStory'),
				id
			])
			redirect(action: "list")
			return
		}

		if (version != null) {
			if (userStoryInstance.version > version) {
				userStoryInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
						[
							message(code: 'userStory.label', default: 'UserStory')] as Object[],
						"Another user has updated this UserStory while you were editing")
				render(view: "edit", model: [userStoryInstance: userStoryInstance])
				return
			}
		}

		userStoryInstance.properties = params

		if (!userStoryInstance.save(flush: true)) {
			render(view: "edit", model: [userStoryInstance: userStoryInstance])
			return
		}

		flash.message = message(code: 'default.updated.message', args: [
			message(code: 'userStory.label', default: 'UserStory'),
			userStoryInstance.id
		])
		redirect(action: "show", id: userStoryInstance.id)
	}

	def delete(Long id) {
		def userStoryInstance = UserStory.get(id)
		if (!userStoryInstance) {
			flash.message = message(code: 'default.not.found.message', args: [
				message(code: 'userStory.label', default: 'UserStory'),
				id
			])
			redirect(action: "list")
			return
		}

		try {
			userStoryInstance.delete(flush: true)
			flash.message = message(code: 'default.deleted.message', args: [
				message(code: 'userStory.label', default: 'UserStory'),
				id
			])
			redirect(action: "list")
		}
		catch (DataIntegrityViolationException e) {
			flash.message = message(code: 'default.not.deleted.message', args: [
				message(code: 'userStory.label', default: 'UserStory'),
				id
			])
			redirect(action: "show", id: id)
		}
	}

	def jsonFindUserStoryByProject() {
		def projectID = request.getParameter("projectID");
		def userStories = userStoryService.findUserStoryByProject(projectID);
		if(userStories) {
			render(contentType : "text/json", {[userStories : userStories, result : "OK"]})
			response.status = 200
		} else {
			render(contentType : "text/json", {[result : "No Data"]})
			response.status = 200
		}
	}
}
